package com.ybkj.o2o.mc.common;

import com.ybkj.o2o.mc.entity.AccountBase;
import com.ybkj.o2o.mc.entity.PassengerAccount;

/**
 * Created by shine on 16/9/20.
 */
public class AccountUtils {

  /**
   * 账户充值计算工具类.
   *
   * @param account        充值记录
   * @param entity         账户信息
   * @param useNoDrawMoney 优先使用不可提现金额
   */
  public static void recharge(PassengerAccount account,
                              AccountBase entity,
                              boolean useNoDrawMoney) {

    if (useNoDrawMoney && account.getMoney() < 0) { //充值金额为负，扣钱
      //使用不可提现金额冲抵
      double balance = Arith.add(entity.getNodrawMoney(), account.getMoney());

      if (balance >= 0) {
        entity.setNodrawMoney(balance);
      } else {
        balance = Arith.add(entity.getDrawMoney(), balance);
        entity.setDrawMoney(balance);
        entity.setNodrawMoney(0d);
      }
    } else {

      if (account.getMoney() < 0) { //充值金额为负，扣钱
        //优先使用可提现金额
        double balance = Arith.add(entity.getDrawMoney(), account.getMoney());

        if (balance >= 0) {
          entity.setDrawMoney(balance);
        } else {

          double totalBalance = entity.getNodrawMoney();

          if (entity.getDrawMoney() > 0) {
            totalBalance = Arith.add(totalBalance, entity.getDrawMoney()); //总余额
          }

          balance = Arith.add(totalBalance, account.getMoney()); //总余额扣除

          if (balance >= 0) { //扣除后为正
            entity.setNodrawMoney(balance);
            if (entity.getDrawMoney() > 0) {
              entity.setDrawMoney(0d);
            }
          } else { //扣除后为负
            entity.setNodrawMoney(0d);
            if (entity.getDrawMoney() >= 0) {
              entity.setDrawMoney(balance);
            } else {

              entity.setDrawMoney(Arith.add(balance, entity.getDrawMoney()));
            }
          }
        }
      } else {
        
        double balance = Arith.add(entity.getDrawMoney(), account.getMoney());
        
        entity.setDrawMoney(balance);
      }
    }
  }

}
